home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / ddx / sprite.X11R3 / RCS / spriteMouse.c,v < prev   
Encoding:
Text File  |  1989-10-13  |  8.5 KB  |  352 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     87.06.20.19.56.48;  author deboor;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     87.06.16.12.21.21;  author deboor;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @mouse interface
  22. ,
  23. @
  24.  
  25.  
  26. 1.2
  27. log
  28. @adapted to Beta-0
  29. @
  30. text
  31. @/*-
  32.  * spriteMouse.c --
  33.  *    Functions for playing cat and mouse... sorry.
  34.  *
  35.  * Copyright (c) 1987 by the Regents of the University of California
  36.  *
  37.  * Permission to use, copy, modify, and distribute this
  38.  * software and its documentation for any purpose and without
  39.  * fee is hereby granted, provided that the above copyright
  40.  * notice appear in all copies.  The University of California
  41.  * makes no representations about the suitability of this
  42.  * software for any purpose.  It is provided "as is" without
  43.  * express or implied warranty.
  44.  *
  45.  *
  46.  */
  47. #ifndef lint
  48. static char rcsid[] =
  49.     "$Header: spriteMouse.c,v 1.1 87/06/16 12:21:21 deboor Exp $ SPRITE (Berkeley)";
  50. #endif lint
  51.  
  52. #define NEED_EVENTS
  53. #include    "spriteddx.h"
  54.  
  55. typedef struct {
  56.     int        bmask;        /* Current button state */
  57.     Bool    mouseMoved;        /* Mouse has moved */
  58. } SunMsPrivRec, *SunMsPrivPtr;
  59.  
  60. static void           spriteMouseCtrl();
  61. static int           spriteMouseGetMotionEvents();
  62. static void           spriteMouseProcessEvent();
  63. static void           spriteMouseDoneEvents();
  64.  
  65. /*
  66.  * Because lastEventTime is actual time, while event times are since-boot
  67.  * times, we keep the time of the most recent mouse event for
  68.  * the DoneEvents procedure to use.
  69.  */
  70. static int            lastMouseEventTime = 0;
  71.  
  72. static SunMsPrivRec    spriteMousePriv;
  73. PtrPrivRec     sysMousePriv = {
  74.     -1,                /* Descriptor to device */
  75.     (Dev_KbdEvent *(*)())NoopDDA,/* Function to read events -- done by
  76.                  * keyboard */
  77.     spriteMouseProcessEvent,    /* Function to process an event */
  78.     spriteMouseDoneEvents,    /* When all the events have been */
  79.                 /* handled, this function will be */
  80.                 /* called. */
  81.     0,                /* Current X coordinate of pointer */
  82.     0,                /* Current Y coordinate */
  83.     NULL,            /* Screen pointer is on */
  84.     (pointer)&spriteMousePriv,    /* Field private to device */
  85. };
  86.  
  87. /*-
  88.  *-----------------------------------------------------------------------
  89.  * spriteMouseProc --
  90.  *    Handle the initialization, etc. of a mouse
  91.  *
  92.  * Results:
  93.  *    none.
  94.  *
  95.  * Side Effects:
  96.  *
  97.  *-----------------------------------------------------------------------
  98.  */
  99. int
  100. spriteMouseProc (pMouse, what)
  101.     DevicePtr      pMouse;       /* Mouse to play with */
  102.     int              what;            /* What to do with it */
  103. {
  104.     BYTE          map[4];
  105.  
  106.     switch (what) {
  107.     case DEVICE_INIT:
  108.         if (pMouse != LookupPointerDevice()) {
  109.         ErrorF ("Cannot open non-system mouse");    
  110.         return (!Success);
  111.         }
  112.         
  113.         sysMousePriv.pScreen = &screenInfo.screen[0];
  114.         sysMousePriv.x = sysMousePriv.pScreen->width / 2;
  115.         sysMousePriv.y = sysMousePriv.pScreen->height / 2;
  116.  
  117.         spriteMousePriv.bmask = 0x87;   /* All buttons up */
  118.         spriteMousePriv.mouseMoved = FALSE;
  119.  
  120.         pMouse->devicePrivate = (pointer) &sysMousePriv;
  121.         pMouse->on = FALSE;
  122.         map[1] = 1;
  123.         map[2] = 2;
  124.         map[3] = 3;
  125.         InitPointerDeviceStruct(
  126.         pMouse, map, 3, spriteMouseGetMotionEvents, spriteMouseCtrl);
  127.         break;
  128.     case DEVICE_ON:
  129.         break;
  130.     case DEVICE_CLOSE:
  131.     case DEVICE_OFF:
  132.         break;
  133.     }
  134.     return (Success);
  135. }
  136.         
  137. /*-
  138.  *-----------------------------------------------------------------------
  139.  * spriteMouseCtrl --
  140.  *    Alter the control parameters for the mouse. Since acceleration
  141.  *    etc. is done from the PtrCtrl record in the mouse's device record,
  142.  *    there's nothing to do here.
  143.  *
  144.  * Results:
  145.  *    None.
  146.  *
  147.  * Side Effects:
  148.  *    None.
  149.  *
  150.  *-----------------------------------------------------------------------
  151.  */
  152. static void
  153. spriteMouseCtrl (pMouse)
  154.     DevicePtr      pMouse;
  155. {
  156. }
  157.  
  158. /*-
  159.  *-----------------------------------------------------------------------
  160.  * spriteMouseGetMotionEvents --
  161.  *    Return the (number of) motion events in the "motion history
  162.  *    buffer" (snicker) between the given times.
  163.  *
  164.  * Results:
  165.  *    The number of events stuffed.
  166.  *
  167.  * Side Effects:
  168.  *    The relevant xTimecoord's are stuffed in the passed memory.
  169.  *
  170.  *-----------------------------------------------------------------------
  171.  */
  172. static int
  173. spriteMouseGetMotionEvents (buff, start, stop)
  174.     CARD32 start, stop;
  175.     xTimecoord *buff;
  176. {
  177.     return 0;
  178. }
  179.  
  180. /*-
  181.  *-----------------------------------------------------------------------
  182.  * MouseAccelerate --
  183.  *    Given a delta and a mouse, return the acceleration of the delta.
  184.  *
  185.  * Results:
  186.  *    The corrected delta
  187.  *
  188.  * Side Effects:
  189.  *    None.
  190.  *
  191.  *-----------------------------------------------------------------------
  192.  */
  193. static short
  194. MouseAccelerate (pMouse, delta)
  195.     DevicePtr      pMouse;
  196.     int              delta;
  197. {
  198.     register int  sgn = sign(delta);
  199.     register PtrCtrl *pCtrl;
  200.  
  201.     delta = abs(delta);
  202.     pCtrl = &((DeviceIntPtr) pMouse)->u.ptr.ctrl;
  203.  
  204.     if (delta > pCtrl->threshold) {
  205.     return ((short) (sgn * (pCtrl->threshold +
  206.                 ((delta - pCtrl->threshold) * pCtrl->num) /
  207.                 pCtrl->den)));
  208.     } else {
  209.     return ((short) (sgn * delta));
  210.     }
  211. }
  212.  
  213. /*-
  214.  *-----------------------------------------------------------------------
  215.  * spriteMouseProcessEvent --
  216.  *    Given a Firm_event for a mouse, pass it off the the dix layer
  217.  *    properly converted...
  218.  *
  219.  * Results:
  220.  *    None.
  221.  *
  222.  * Side Effects:
  223.  *    The cursor may be redrawn...? devPrivate/x/y will be altered.
  224.  *
  225.  *-----------------------------------------------------------------------
  226.  */
  227. static void
  228. spriteMouseProcessEvent (pMouse, ev)
  229.     DevicePtr      pMouse;       /* Mouse from which the event came */
  230.     Dev_KbdEvent  *ev;            /* Event to process */
  231. {
  232.     xEvent        xE;
  233.     register PtrPrivPtr    pPriv;    /* Private data for pointer */
  234.     register SunMsPrivPtr pSunPriv; /* Private data for mouse */
  235.     register int      bmask;    /* Temporary button mask */
  236.     register int      button;
  237.  
  238.     pPriv = (PtrPrivPtr)pMouse->devicePrivate;
  239.     pSunPriv = (SunMsPrivPtr) pPriv->devPrivate;
  240.  
  241.     xE.u.keyButtonPointer.time = ev->time;
  242.  
  243.     bmask = ev->key ^ pSunPriv->bmask;
  244.  
  245.     if (ev->deltaX) {
  246.     pPriv->x += MouseAccelerate (pMouse, ev->deltaX);
  247.     if (spriteConstrainXY (&pPriv->x, &pPriv->y)) {
  248.         pSunPriv->mouseMoved = TRUE;
  249.     }
  250.     }
  251.  
  252.     if (ev->deltaY) {
  253.     pPriv->y -= MouseAccelerate (pMouse, ev->deltaY);
  254.     if (spriteConstrainXY (&pPriv->x, &pPriv->y)) {
  255.         pSunPriv->mouseMoved = TRUE;
  256.     }
  257.     }
  258.     
  259.     if (ev->key ^ pSunPriv->bmask) {
  260.     spriteMouseDoneEvents (pMouse, FALSE);
  261.     }
  262.     xE.u.keyButtonPointer.rootX = pPriv->x;
  263.     xE.u.keyButtonPointer.rootY = pPriv->y;
  264.     xE.u.keyButtonPointer.time = ev->time;
  265.  
  266.     for (bmask = 4, button = 1; bmask != 0; bmask >>= 1, button++) {
  267.     if ((ev->key & bmask) != (pSunPriv->bmask & bmask)) {
  268.         xE.u.u.type = (ev->key & bmask) ? ButtonRelease : ButtonPress;
  269.         xE.u.u.detail = button;
  270.  
  271.         (* pMouse->processInputProc) (&xE, pMouse);
  272.     }
  273.     }
  274.     pSunPriv->bmask = ev->key;
  275.     lastMouseEventTime = ev->time;
  276. }
  277.  
  278. /*-
  279.  *-----------------------------------------------------------------------
  280.  * spriteMouseDoneEvents --
  281.  *    Finish off any mouse motions we haven't done yet. (At the moment
  282.  *    this code is unused since we never save mouse motions as I'm
  283.  *    unsure of the effect of getting a keystroke at a given [x,y] w/o
  284.  *    having gotten a motion event to that [x,y])
  285.  *
  286.  * Results:
  287.  *    None.
  288.  *
  289.  * Side Effects:
  290.  *    A MotionNotify event may be generated.
  291.  *
  292.  *-----------------------------------------------------------------------
  293.  */
  294. /*ARGSUSED*/
  295. static void
  296. spriteMouseDoneEvents (pMouse, final)
  297.     DevicePtr      pMouse;
  298.     Bool      final;
  299. {
  300.     PtrPrivPtr      pPriv;
  301.     SunMsPrivPtr  pSunPriv;
  302.     xEvent      xE;
  303.  
  304.     pPriv = (PtrPrivPtr) pMouse->devicePrivate;
  305.     pSunPriv = (SunMsPrivPtr) pPriv->devPrivate;
  306.  
  307.     if (pSunPriv->mouseMoved) {
  308.     spriteMoveCursor (pPriv->pScreen, pPriv->x, pPriv->y);
  309.     xE.u.keyButtonPointer.rootX = pPriv->x;
  310.     xE.u.keyButtonPointer.rootY = pPriv->y;
  311.     xE.u.keyButtonPointer.time = lastMouseEventTime;
  312.     xE.u.u.type = MotionNotify;
  313.     (* pMouse->processInputProc) (&xE, pMouse);
  314.     pSunPriv->mouseMoved = FALSE;
  315.     }
  316. }
  317. @
  318.  
  319.  
  320. 1.1
  321. log
  322. @Initial revision
  323. @
  324. text
  325. @d19 1
  326. a19 1
  327.     "$Header: spriteMouse.c,v 2.5 87/05/13 15:07:14 deboor Exp $ SPRITE (Berkeley)";
  328. d35 7
  329. d43 1
  330. a43 2
  331. static PtrPrivRec     sysMousePriv = {
  332.     DEVICE_MOUSE,        /* Pointer type */
  333. d217 1
  334. a217 1
  335.     if (sunConstrainXY (&pPriv->x, &pPriv->y)) {
  336. d224 1
  337. a224 1
  338.     if (sunConstrainXY (&pPriv->x, &pPriv->y)) {
  339. d238 2
  340. a239 2
  341.         xE.type = (ev->key & bmask) ? ButtonRelease : ButtonPress;
  342.         xE.detail = button;
  343. d245 1
  344. d278 1
  345. a278 1
  346.     sunMoveCursor (pPriv->pScreen, pPriv->x, pPriv->y);
  347. d281 2
  348. a282 2
  349.     xE.u.keyButtonPointer.time = lastEventTime;
  350.     xE.type = MotionNotify;
  351. @
  352.